package com.linkage.jz.bankruptcy.bean;

import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.linkage.jz.bankruptcy.utils.UploadImageConverter;
import com.linkage.seapeak.pojo.UploadImage;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class SerializedZQApplyFileList implements Parcelable {

    private String hasEffectiveJudgmentJson;
    private String hasApplyExecutionJson;
    private String hasOtherDeclarationFileJson;
    private final Gson gson = new Gson();
    private final Type mType = new TypeToken<List<UploadImage>>(){}.getType();

    private final UploadImageConverter<TaskAtta> mConverter = new UploadImageConverter<TaskAtta>() {
        @Override
        public UploadImage toUploadImage(TaskAtta taskAtta) {
            UploadImage image = new UploadImage();
            image.setFilename(taskAtta.getAttachmentName());
            image.setFilesize(taskAtta.getAttachmentSize());
            image.setFilepath(taskAtta.getAttachmentDownloadAddress());
            image.setLoadUrl(taskAtta.getAttachmentPreviewAddress());
            return image;
        }

        @Override
        public TaskAtta fromUploadImage(UploadImage image) {
            TaskAtta taskAtta = new TaskAtta();
            taskAtta.setAttachmentName(image.getFilename());
            taskAtta.setAttachmentSize(image.getFilesize());
            taskAtta.setAttachmentDownloadAddress(image.getFilepath());
            taskAtta.setAttachmentPreviewAddress(image.getLoadUrl());
            return null;
        }
    };

    public SerializedZQApplyFileList(ZQBaseProjectCrDecInfo info) {
        this.hasEffectiveJudgmentJson = convertJson(info.getJudgmentAtta());
        this.hasApplyExecutionJson = convertJson(info.getExecutionAtta());
        this.hasOtherDeclarationFileJson = convertJson(info.getOtherAtta());
    }

    protected SerializedZQApplyFileList(Parcel in) {
        hasEffectiveJudgmentJson = in.readString();
        hasApplyExecutionJson = in.readString();
        hasOtherDeclarationFileJson = in.readString();
    }

    public static final Creator<SerializedZQApplyFileList> CREATOR = new Creator<SerializedZQApplyFileList>() {
        @Override
        public SerializedZQApplyFileList createFromParcel(Parcel in) {
            return new SerializedZQApplyFileList(in);
        }

        @Override
        public SerializedZQApplyFileList[] newArray(int size) {
            return new SerializedZQApplyFileList[size];
        }
    };

    private String convertJson(List<TaskAtta> attas) {
        if (attas == null) return "[]";
        else {
            List<UploadImage> uploadImages = new ArrayList<>(attas.size());
            for (TaskAtta atta : attas) {
                uploadImages.add(mConverter.toUploadImage(atta));
            }
            return gson.toJson(uploadImages, mType);
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(hasEffectiveJudgmentJson);
        dest.writeString(hasApplyExecutionJson);
        dest.writeString(hasOtherDeclarationFileJson);
    }

    public String getHasEffectiveJudgmentJson() {
        return hasEffectiveJudgmentJson;
    }

    public void setHasEffectiveJudgmentJson(String hasEffectiveJudgmentJson) {
        this.hasEffectiveJudgmentJson = hasEffectiveJudgmentJson;
    }

    public String getHasApplyExecutionJson() {
        return hasApplyExecutionJson;
    }

    public void setHasApplyExecutionJson(String hasApplyExecutionJson) {
        this.hasApplyExecutionJson = hasApplyExecutionJson;
    }

    public String getHasOtherDeclarationFileJson() {
        return hasOtherDeclarationFileJson;
    }

    public void setHasOtherDeclarationFileJson(String hasOtherDeclarationFileJson) {
        this.hasOtherDeclarationFileJson = hasOtherDeclarationFileJson;
    }
}
